# /usr/bin/python
# -*- coding: utf-8 -*-

import os,sys
BASE_DIR = os.path.dirname(os.path.abspath(os.curdir))
sys.path.append(BASE_DIR)
import re

def key_trn(key):

    key_dict = {
    "kernel.maxfiles": "kernel.maxfiles",
    "kernel.maxproc": "kernel.maxprox",
    "vm.memory.size": memory,
    "proc.num": proc,
    "system.swap.size": memory,
    "system.cpu.load": "cpu.busy",
    "system.cpu.util": cpu,
    "net.tcp.listen": listen,
    "FromDual.MySQL.server.disk_space": disk
    }

    try:
        new_key = key_dict[key.split('[')[0]]
        if isinstance(new_key,basestring):
            return new_key
        else:
            return new_key(key)
    except KeyError:
        raise KeyError("not support the key %s"%key)

def cpu(key):
    if re.match(r"system.cpu.util",key):
        base = re.split(r"\[|\]|,",key)[-2]
        if base == "idle":
            return "cpu.idle"
        elif base == "iowait":
            return "cpu.iowait"
        elif base == "user":
            return "cpu.user"
        elif base == "softirq":
            return "cpu.irq"
        elif base == "steal":
            return "cpu.steal"
        else:
            raise KeyError("not support the key %s"%key)
    else:
        raise KeyError("not support the key %s"%key)

def memory(key):
    if re.match(r"vm.memory.size",key):
        base = re.split(r"\[|\]|,",key)[-2]
        if base == "total":
            return "mem.memtotal"
        elif base == "free":
            return "mem.memfree"
        elif base == "pfree":
            return "mem.memfree.percent"
        elif base == "used":
            return "mem.memused"
        elif base == "pused":
            return "mem.memused.percent"
        elif base == "available":
            return "mem.memfree"
        else:
            raise KeyError("not support the key %s"%key)

    elif re.match(r"system.swap.size",key):
        base = re.split(r"\[|\]|,",key)[-2]
        if base == "total":
            return "mem.swaptotal"
        elif base == "free":
            return "mem.swapfree"
        elif base == "pfree":
            return "mem.swapfree.percent"
        elif base == "used":
            return "mem.swapused"
        elif base == "pused":
            return "mem.swapused.percent"
        else:
            raise KeyError("not support the key %s"%key)
    else:
        raise KeyError("not support the key %s"%key)

def proc(key):
    if re.match(r"proc.num",key):
        base = re.split(r"\[|\]|,",key)
        if len(base) == 3:
            return ["proc.num","name="+base[-2]]
        elif len(base) == 6:
            return ["proc.num","cmdline="+base[-2]]
        else:
            raise KeyError("not support the key %s"%key)
    else:
        raise KeyError("not support the key %s"%key)

def listen(key):
    if re.match(r"net.tcp.listen",key):
        base = re.split(r"\[|\]|,",key)[-2]
        return ["net.port.listen","port="+base]
    else:
        raise KeyError("not support the key %s"%key)

def disk(key):
    if re.match(r"FromDual.MySQL.server.disk_space",key):
        base = re.split(r"\[|\]|,",key)
        if base[-2] == "pfree":
            return ["df.bytes.free.percent","mount="+base[-3]]
        if base[-2] == "free":
            return ["df.bytes.free","mount="+base[-3]]
        else:
            raise KeyError("not support the key %s"%key)
    else:
        raise KeyError("not support the key %s"%key)